package com.study.seata.business.service.impl;

import com.study.seata.business.feign.AccountService;
import com.study.seata.business.feign.OrderService;
import com.study.seata.business.feign.StorageService;
import com.study.seata.business.service.BusinessService;
import com.study.seata.common.model.account.Account;
import com.study.seata.common.model.order.Order;
import com.study.seata.common.model.storage.Storage;
import com.study.seata.common.utils.CommonUtils;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 业务service实现
 * 
 * @author
 */
@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {


	@Autowired
	private AccountService accountService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private StorageService storageService;

	/**
	 * 付款
	 *
	 * @param accountId
	 * @param orderId
	 * @param storageId
	 * @return
	 */
	@GlobalTransactional
	@Override
	public Map<String, Object> purchase(String accountId, String orderId, String storageId) {
		System.out.println("accountId:" + accountId+",orderId:" + orderId+",storageId:" + storageId);

		Storage storage = new Storage();
		storage.setStorageId(CommonUtils.genUUID());
		storage.setStorageName("name");
		storage.setStorageCount(20);
		storage.setRemark("备注");
		storage.setLogicDel("N");

		Order order = new Order();
		order.setOrderId(CommonUtils.genUUID());
		order.setOrderNo("NO" + System.currentTimeMillis());
		order.setOrderDetail("详情");
		order.setCreateTime(System.currentTimeMillis());
		order.setRemark("备注");
		order.setLogicDel("N");
		Account account = new Account();
		account.setAccountId(CommonUtils.genUUID());
		account.setAccountName("name");
		account.setAmount(100L);
		account.setLogicDel("N");
		account.setRemark("备注");
		try {
			final String xid = RootContext.getXID();
			RootContext.bind(xid);
			System.out.println("xid:" + xid);

			Map<String, Object> insert = storageService.insert(storage);
			if ((int) insert.get("status") != 200) {
				throw new RuntimeException((String) insert.get("message"));
			}
			Map<String, Object> insert2 = orderService.insert(order);
			if ((int) insert2.get("status") != 200) {
				throw new RuntimeException((String) insert2.get("message"));
			}

//			Map<String, Object> insert3 = accountService.insert(account);
//			if ((int) insert3.get("status") != 200) {
//				throw new RuntimeException((String) insert3.get("message"));
//			}

			account.setAccountId("zhangsan");
			account.setRemark("测试修改");
			Map<String, Object> update = accountService.update(account);
			if ((int) update.get("status") != 200) {
				throw new RuntimeException((String) update.get("message"));
			}

			account.setAccountId("zhangsan");
			Map<String, Object> delete = accountService.delete(account);
			if ((int) delete.get("status") != 200) {
				throw new RuntimeException((String) delete.get("message"));
			}


			Map<String, Object> result = new HashMap<>(16);
			result.put("status", 200);
			result.put("message", "付款成功！");
			return result;
		} catch (Exception e) {
			log.error(ExceptionUtils.getStackTrace(e));
			throw new RuntimeException(e);
		}
	}
}